import 'package:flutter/material.dart';

//对话框练习
class AlertDialogDemo1 extends StatefulWidget {
  const AlertDialogDemo1({Key? key}) : super(key: key);

  @override
  _AlertDialogDemo1State createState() => _AlertDialogDemo1State();
}

class _AlertDialogDemo1State extends State<AlertDialogDemo1> {
  List<int> _list = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    for (var i = 0; i <= 60; i++) {
      _list.add(i);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('弹窗练习'),
      ),
      body: Center(
        child: Column(
          children: [
            ElevatedButton(
              onPressed: _showAlert,
              child: Text('弹窗'),
            ),
            ElevatedButton(
              onPressed: _showListAlert,
              child: Text('列表弹窗'),
            )
          ],
        ),
      ),
    );
  }

  void _showAlert() async {
    //1、使用showDialog才能显示
    //2、传递context上下文对象
    //关闭弹窗就是等于关闭一个页面
    var result = await showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            title: Text('标题'),
            content: Text('确认操作吗？'),
            actions: [
              TextButton(
                  onPressed: () {
                    Navigator.of(context).pop(false);
                  },
                  child: Text('取消')),
              TextButton(
                  onPressed: () {
                    Navigator.pop(context, true);
                  },
                  child: Text('确认'))
            ],
          );
        });
    print('默认弹窗，打印返回值');
    print(result);
  }

  void _showListAlert() async {
    var result = await showDialog(
        barrierDismissible:false,//屏障可关闭？
        context: context,
        builder: (BuildContext context) {
          return SimpleDialog(
            title: Text('请选择'),
            children: _list
                .map((e) => GestureDetector(
                      child: Text(e.toString()),
                      onTap: () {
                        Navigator.pop(context, e);
                      },
                    ))
                .toList(),
          );
        });
    print('列表弹窗，打印返回值');

    print(result);
  }
}
